package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.LongMenUtils;
import com.ruoyi.mes.vo.buy.BuyInHouVo;
import com.ruoyi.mes.vo.product.BomMtrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IRevokePickService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.core.domain.AjaxResult.DATA_TAG;

/**
 * 领料返回Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-05-16
 */
@Service
public class RevokePickServiceImpl implements IRevokePickService {
    @Autowired
    private MtrStockMapper mtrStockMapper;

    @Autowired
    private HouInfoMapper houInfoMapper;

    @Autowired
    private RevokePickMapper revokePickMapper;

    @Autowired
    private PickInfoMapper pickInfoMapper;

    @Autowired
    private WkspTaskServiceImpl wkspTaskService;

    @Autowired
    private LocatorInfoMapper locatorInfoMapper;

    /**
     * 查询领料返回
     *
     * @param id 领料返回主键
     * @return 领料返回
     */
    @Override
    public RevokePick selectRevokePickById(Long id) {
        return revokePickMapper.selectRevokePickById(id);
    }

    /**
     * 查询领料返回列表
     *
     * @param revokePick 领料返回
     * @return 领料返回
     */
    @Override
    public List<RevokePick> selectRevokePickList(RevokePick revokePick) {
        return revokePickMapper.selectRevokePickList(revokePick);
    }

    /**
     * 新增领料返回
     *
     * @param revokePick 领料返回
     * @return 结果
     */
    @Override
    public int insertRevokePick(RevokePick revokePick) {
        revokePick.setCreateTime(DateUtils.getNowDate());
        return revokePickMapper.insertRevokePick(revokePick);
    }

    /**
     * 修改领料返回
     *
     * @param revokePick 领料返回
     * @return 结果
     */
    @Override
    public int updateRevokePick(RevokePick revokePick) {
        revokePick.setUpdateTime(DateUtils.getNowDate());
        return revokePickMapper.updateRevokePick(revokePick);
    }

    /**
     * 批量删除领料返回
     *
     * @param ids 需要删除的领料返回主键
     * @return 结果
     */
    @Override
    public int deleteRevokePickByIds(Long[] ids) {
        return revokePickMapper.deleteRevokePickByIds(ids);
    }

    /**
     * 删除领料返回信息
     *
     * @param id 领料返回主键
     * @return 结果
     */
    @Override
    public int deleteRevokePickById(Long id) {
        return revokePickMapper.deleteRevokePickById(id);
    }


    /**
     * 入库
     * @param buyInHouVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult inHou(BuyInHouVo buyInHouVo) {
        RevokePick revokePick = revokePickMapper.selectRevokePickById(buyInHouVo.getId());

        //判断库房剩余容量是否充足，不充足则返回报错信息
        HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(buyInHouVo.getHouId());
        if (houInfo.getHouRemainCapacity() < revokePick.getRevokeQuantity()){
            return AjaxResult.error("库房剩余数量不足!");
        }

        //调取龙门,根据库位进行放料
        LongMenUtils.LongMenPutWithLocator(buyInHouVo.getLocatorInfoId());

        //库房剩余容量充足，则更新库房及物料库存的数量
        houInfo.setHouRemainCapacity(houInfo.getHouRemainCapacity() - revokePick.getRevokeQuantity());
        houInfoMapper.updateHouInfo(houInfo);
        //更新库位
        List<LocatorInfo> locatorBySerialCode = locatorInfoMapper.getLocatorBySerialCode(revokePick.getMtrCode());
        LocatorInfo locatorInfo = locatorBySerialCode.get(0);
        locatorInfo.setRemainCapacity(locatorInfo.getRemainCapacity() - revokePick.getRevokeQuantity());
        locatorInfoMapper.updateLocatorInfo(locatorInfo);

        //若 库房为新增的 未添加任何物料 则新增一条记录 即判断 mtrStock是否为空
        MtrStock mtrStock = mtrStockMapper.selectMtrStockByHouIdaAndMtrCode(buyInHouVo.getHouId(), revokePick.getMtrCode());
        if (mtrStock != null){//找的到已存在 仓库存放物料
            mtrStock.setMtrQuantity(mtrStock.getMtrQuantity() + revokePick.getRevokeQuantity());
            mtrStockMapper.updateMtrStock(mtrStock);
        }else {
            MtrStock stock = new MtrStock();
            stock.setHouId(buyInHouVo.getHouId());
            stock.setMtrCode(revokePick.getMtrCode());
            stock.setMtrName(revokePick.getMtrName());
            stock.setMtrQuantity(revokePick.getRevokeQuantity());
            mtrStockMapper.insertMtrStock(stock);
        }

        revokePick.setStatus(1L);
        revokePickMapper.updateRevokePick(revokePick);

        return AjaxResult.success("入库成功!");
    }

    /**
     * 返回物料
     * @param wkspTask  车间任务
     */
    @Override
    @Transactional
    public void revokeMtr(WkspTask wkspTask) {
        //若已领料,新增领料返回数据，再删车间任务
        List<PickInfo> pickInfos = pickInfoMapper.selectPickInfoByWkspTaskId(wkspTask.getId());
        String pickCode = pickInfos.get(0).getPickCode();

        BomMtrVo bomMtrVo = new BomMtrVo();
        Object o = wkspTaskService.pickingList(wkspTask.getId()).get(DATA_TAG);
        BeanUtils.copyProperties(o,bomMtrVo);//查询领料清单 放入集合

        for (MtrInfo mtr : bomMtrVo.getMtrs()) {
            RevokePick revokePick = new RevokePick();
            revokePick.setWkspTaskId(wkspTask.getId());
            revokePick.setWkspCode(wkspTask.getWkspCode());
            revokePick.setPickCode(pickCode);
            revokePick.setStatus(0L);
            revokePick.setMtrCode(mtr.getMtrCode());
            revokePick.setMtrName(mtr.getMtrName());
            revokePick.setRevokeQuantity(mtr.getMtrQuantity());
            revokePickMapper.insertRevokePick(revokePick);
        }
    }
}
